package com.pfizer.XmlContentHandler;

import static com.pfizer.Entity.PCR.ACTIVITYINPUTSTRING;
import static com.pfizer.Entity.PCR.ACTIVITYSTRING;
import static com.pfizer.Entity.PCR.COMPLETETIMESTRING;
import static com.pfizer.Entity.PCR.CREATETIMESTRING;
import static com.pfizer.Entity.PCR.DRIDSTRING;
import static com.pfizer.Entity.PCR.DRNAMESTRING;
import static com.pfizer.Entity.PCR.HOSPIDSTRING;
import static com.pfizer.Entity.PCR.HOSPNAMESTRING;
import static com.pfizer.Entity.PCR.OFFDUTYLATSTRING;
import static com.pfizer.Entity.PCR.OFFDUTYLNGSTRING;
import static com.pfizer.Entity.PCR.ONDUTYLATSTRING;
import static com.pfizer.Entity.PCR.ONDUTYLNGSTRING;
import static com.pfizer.Entity.PCR.PCRIDSTRING;
import static com.pfizer.Entity.PCR.PCRSTRING;
import static com.pfizer.Entity.PCR.REMARKSTRING;
import static com.pfizer.Entity.PCR.STATUSSTRING;
import static com.pfizer.Entity.PCR.USERIDSTRING;
import static com.pfizer.Entity.PCR.USERNAMESTRING;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.pfizer.Entity.PCR;

public class PCRContentHandler extends DefaultHandler {
	private List<PCR> pcrs;

	private PCR pcr;
	private StringBuilder sb = new StringBuilder();

	/**
	 * 解析XML數據放于List中，此方法用於外部獲得解析后的數據
	 * 
	 * @return List<PCR>
	 */
	public List<PCR> getPCRs() {
		return pcrs;
	}

	/**
	 * 在根標籤開始時就實例化list用於存放每組數據
	 */
	@Override
	public void startDocument() throws SAXException {
		pcrs = new ArrayList<PCR>();
	}

	/**
	 * 開始解析數據，將得到的數據放於javabean
	 * 
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (ch[0] == '\n' && length == 1) {
			return;
		}
		String sTmp = new String(ch, start, length);
		sTmp = sTmp.trim();
		if (sTmp.length() == 0) {
			return;
		}
		// (2)不管在startElement到endElement的过程中，执行了多少次characters，
		// 都会将内容添加到StringBuilder中，不会丢失内容
		sb.append(sTmp);
	}

	/**
	 * 在一個tag開始時實例化組對象，
	 */
	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {

		if (PCRSTRING.equals(localName)) {

			pcr = new PCR();
		}

	}

	/**
	 * 在一個tag結束后將本組數據放於list
	 */
	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {
		if (PCRSTRING.equals(localName) && pcr != null) {
			pcrs.add(pcr);
			pcr = null;
		} else {
			if (pcr != null) {
				String valueString = sb.toString();
				if (PCRIDSTRING.equals(localName)) {
					pcr.setPCR_ID(Integer.valueOf(valueString));
				} else if (STATUSSTRING.equals(localName)) {
					pcr.setStatus(valueString);
				} else if (USERIDSTRING.equals(localName)) {
					pcr.setUserID(valueString);
				} else if (USERNAMESTRING.equals(localName)) {
					pcr.setUserName(valueString);
				} else if (HOSPIDSTRING.equals(localName)) {
					pcr.setHospital_ID(valueString);
				} else if (HOSPNAMESTRING.equals(localName)) {
					pcr.setHospital_Name(valueString);
				} else if (DRIDSTRING.equals(localName)) {
					pcr.setDr_ID(valueString);
				} else if (DRNAMESTRING.equals(localName)) {
					pcr.setDr_Name(valueString);
				} else if (ACTIVITYSTRING.equals(localName)) {
					pcr.setActivity(valueString);
				} else if (ACTIVITYINPUTSTRING.equals(localName)) {
					pcr.setActivity_Input(valueString);
				} else if (REMARKSTRING.equals(localName)) {
					pcr.setRemark(valueString);
				} else if (ONDUTYLATSTRING.equals(localName)) {
					pcr.setOnDuty_Lat(valueString);
				} else if (ONDUTYLNGSTRING.equals(localName)) {
					pcr.setOnDuty_Lng(valueString);
				} else if (OFFDUTYLATSTRING.equals(localName)) {
					pcr.setOffDuty_Lat(valueString);
				} else if (OFFDUTYLNGSTRING.equals(localName)) {
					pcr.setOffDuty_Lng(valueString);
				} else if (CREATETIMESTRING.equals(localName)) {
					pcr.setCreat_Time(valueString);
				} else if (COMPLETETIMESTRING.equals(localName)) {
					pcr.setComplete_Time(valueString);
				}

			}
		}
		
		sb.delete( 0, sb.length());

	}
}
